<?
/**
 * VoiceOne Configuration
 *
 * Callware VoiceOne is an easy to use web based GUI for the Asterisk PBX. 
 * It gives full control over PBX settings from the admin configuration panel. 
 * You can manage extensions, VoIP trunks, users queues and rules sets, 
 * and dynamically create a IVR.
 * @package voiceone
 */
 
// Sessions are enabled for default and disabled in download scripts, 
// like stream.php for sounds and backups
if((!isset($noSession))||($noSession===false)){
	session_start();
}
// include statements
$dir = dirname(__FILE__) . '/';
require_once $dir . 'config.inc.php';
require_once $dir . 'constants.php';
require_once $dir . '../lib/smarty/libs/Smarty.class.php';
require_once $dir . '../lib/sajax/Sajax.php';
require_once $dir . '../lib/functions.php';
require_once $dir . "../public/lang/".$cwLanguage.".php";
require_once $dir . '../lib/gettext/i18n.php';
// Initialize the Localization-API
T_setlocale(LC_MESSAGES, $cw_lang);
T_bindtextdomain($domain, $absBasePath."/public/lang");
T_bind_textdomain_codeset($domain, $encoding);
T_textdomain($domain);
//create a Smarty object
$smarty = new Smarty;
$smarty->template_dir = $dir.'../public/template/';
$smarty->compile_dir = $dir.'../public/template/cache/';
$smarty->config_dir = $dir.'../public/template/config/';
$smarty->cache_dir = $dir.'../public/template/cache/';
$smarty->plugins_dir = array($dir."../public/plugin/");
$smarty->caching = false;
$smarty->clear_compiled_tpl();
$smarty->assign("cw_public",$relBasePath."public/");
//read the template Translation
foreach($cwlang["template"] as $cwk=>$cwv) $smarty->assign($cwk,$cwv);
//SOAP wrapper ON:create a nuSoap Client
if($soapWrapper===true){
	if(!is_file($webserviceAbsPath."config/config.inc.php")) $asterisk=false;
  else{
    //include all the needed file
    include_once ($webserviceAbsPath."config/cfg.php");
    //Include webservice's functions
    $base_folder=$webserviceAbsPath."modules";
    //die($base_folder);
    if($handle=opendir($base_folder)){
      while(false !== ($file = readdir($handle))){
        if(($file==".")||($file=="..")) continue;
        if((is_dir($base_folder."/".$file))&&(is_file($base_folder."/".$file."/functions/methods.php")) ){
          require_once $base_folder."/".$file."/functions/methods.php";
        }
      }
      closedir($handle);
    }
    require_once dirname(__FILE__) . '/../lib/nusoapWrapper/class.soapclientLocal.php';
    //proxy class wrapper filename
    $proxyClassFile=$absBasePath."../sandbox/wsdl.php";
    //create che proxy class wrapper if doesn't exists and cache it in the sandbox/ folder
    if (!file_exists($proxyClassFile)) {
      $soapClient = new soapclientLocal($soapWsdl, true);
      ini_set("soap.wsdl_cache_enabled", "1"); 
      $err = $soapClient->getError();
      if ($err) die('An error occurred while the WSDL was being accessed: '.$err);
      $localSoapClass=$soapClient->_getLocalProxyClassCode();
      file_put_contents($proxyClassFile,$localSoapClass);  
      error_log("CREATED THE WSDL LOCAL WRAPPER: $proxyClassFile");
    }
    //read the proxy class wrapper and evaluate it
    $proxyClass=file_get_contents($proxyClassFile);
  	eval($proxyClass);
  	//eval("\$asterisk = new soap_proxy();");
  	eval("\$asterisk = new nusoap_proxy();");
  }
}
//SOAP wrapper OFF
else{
  require_once dirname(__FILE__) . '/../lib/nusoap/nusoap.php';
  //$soapClient = new soapclient($soapWsdl, true);
  $soapClient = new nusoap_client($soapWsdl, true);
  ini_set("soap.wsdl_cache_enabled", "1"); 
  $err = $soapClient->getError();
  if ($err) die('An error occurred while the WSDL was being accessed: '.$err);
  $asterisk=$soapClient->getProxy();
  $asterisk->setCredentials($soapUserName,$soapPassword);
  $asterisk->useHTTPPersistentConnection();
}
//bypass the register globals setting
if (!ini_get('register_globals')) {
  $superglobals = array($_SERVER, $_ENV, $_FILES, $_COOKIE, $_POST, $_GET);
  if (isset($_SESSION)) extract($_SESSION);
  foreach ($superglobals as $superglobal) extract($superglobal, EXTR_SKIP);
}
//if setup is running don't check user login and return;
if (strstr($_SERVER["PHP_SELF"],"setup.php")!==false) return;
//check for authentication (don't ask if session is turned off -> see row 14)
if(($_COOKIE["cwUserId"]=="")&&(($_SESSION["cwUserId"]=="")&&((!isset($noSession))||($noSession===false)))){
  //  no session and no cookie - user must login first
  if (isset($_POST["user_name"])) {
    // user has typed his name and password
    $uid=checkUserAccount($_POST["user_name"],$_POST["password"]);
    if ($uid>0) {
      // user account is valid. So the user can request to set a cookie
      if ($_POST["remember_me"]=="1") {
          setcookie ("cwUserId", $uid,time()+3600*24*365,"/");
      }
      else {
          // if no cookie, than we set the session id
          $_SESSION['cwUserId'] = $uid;
      }
      // redirect to index.php and exit
      header ("location: index.php");
      die();
    } else {
        // user account is invalid - set errortext
        $alert="<div class=\"AlertBad\">" . $cwlang["generic"]["loginErrror"]. "</div>";
    }
  }  // end if  post username

  // set header to 403
  header('HTTP/1.0 401 Unauthorized');
  header('Status: 401 Unauthorized');
  $smarty->assign("alert", $alert);
  $smarty->assign("contents", $contents);
  $smarty->display('login.tpl');
  die;
}
//get User Information
$cwUserInfo=@array_pop($asterisk->getUser($cwUserId));
if(is_array($cwUserInfo)) $smarty->assign("cwUser", $cwUserInfo["username"]);
//set the user language
if (trim($cwUserInfo["lang"])!="") $_SESSION["cwUserLang"]=$cwUserInfo["lang"]; 
else $_SESSION["cwUserLang"]=$cwLanguage;
//include the new user language 
require_once (dirname(__FILE__)."/../public/lang/".$_SESSION["cwUserLang"].".php"); 
foreach($cwlang["template"] as $cwk=>$cwv) $smarty->assign($cwk,$cwv); 
$cwLanguage=$_SESSION["cwUserLang"];
//check the pbx status
$ret=$asterisk->pbxSwitch("status");
if ($ret=="ON") $smarty->assign("pbxStatus", $relBasePath."/public/img/tl-green-small.gif");
else $smarty->assign("pbxStatus", $relBasePath."/public/img/tl-red-small.gif");
//set the hostname
if(!isset($_SESSION["hostname"])){
  $systemOptionArr=$asterisk->getOptionByName("hostname","SYSTEM");
  if((is_array($systemOptionArr))&&(is_array($systemOptionArr[0]))){
    $_SESSION["hostname"]=$systemOptionArr[0]["value"];
  }
}
$smarty->assign("hostname", $_SESSION["hostname"]);
//set system version
if(!isset($_SESSION["voiceone_version"])){
  $systemOptionArr=$asterisk->getOptionByName("voiceone_version","SYSTEM");
  if((is_array($systemOptionArr))&&(is_array($systemOptionArr[0]))){
    $_SESSION["voiceone_version"]=$systemOptionArr[0]["value"];
  }
}
$smarty->assign("voiceone_version", $_SESSION["voiceone_version"]);
//logout
if (isset($_GET["cwLogOut"]) or !is_array($cwUserInfo) ) {
  setcookie ("cwUserId", $uid,time()-3600,"/");
  unset($_SESSION['cwUserId']);
  header ("location: index.php");
  die();
}
?>